﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 日铁.Model;
using WindowsFormsApp1.Model;

namespace 日铁.UI.SqlCe
{
    public partial class SqlLongin : Form
    {
        public SqlLongin()
        {
            InitializeComponent();
        }

        //获取IP
        private void button2_Click(object sender, EventArgs e)
        {
            IPAddress pAddress = Address();
            textBox1.Text = pAddress.ToString();
        }
        public IPAddress Address()
        {
            IPAddress iPAddress = null;
            IPAddress[] pAddresses = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress pAddress in pAddresses)
            {
                if (pAddress.AddressFamily == AddressFamily.InterNetwork)
                {
                    iPAddress = pAddress;
                }
            }
            return iPAddress;
        }
        //连接数据库
        private void button1_Click(object sender, EventArgs e)
        {
            var connectionString =
                $@"Data Source={textBox1.Text};Initial Catalog={textBox2.Text};Persist Security Info=True;User ID={textBox3.Text};Password={textBox4.Text}";
            try
            {
                var conn = new System.Data.SqlClient.SqlConnection(connectionString);
                conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    ZhidingY.SQLIP = textBox1.Text;
                    ZhidingY.SQLName = textBox2.Text;
                    ZhidingY.SQLK = textBox3.Text;
                    ZhidingY.SQLmm = textBox4.Text;

                    // 连接成功
                    MessageBox.Show("成功连接到数据库");
                    conn.Close();
                }
                else
                {
                    MessageBox.Show("连接到数据库失败！使用连接测试重连数据库");
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Com_zhang.G_BAUDRATE = textBox1.Text;
            Com_zhang.G_DATABITS = textBox2.Text;
            Com_zhang.G_STOP = textBox3.Text;
            Com_zhang.G_PARITY = textBox4.Text;
            Com_zhang.SaveProfile();
        }

        private void SqlLongin_Load(object sender, EventArgs e)
        {
            Com_zhang.LoadProfile();
            textBox1.Text=Com_zhang.G_BAUDRATE ;
             textBox2.Text = Com_zhang.G_DATABITS;
            textBox3.Text = Com_zhang.G_STOP;
            textBox4.Text = Com_zhang.G_PARITY;
        }
    }
}
